package olga.gorbatiuk.journal.impl;

import static org.junit.Assert.*;
import olga.gorbatiuk.journal.Journal;
import olga.gorbatiuk.record.Record;

import org.junit.Test;

public class CollectionJournalTest {

    @Test
    public void add() {
	String message = "2014-11-10 20:30:40 ONE источник_сообщение";
	Record record = new Record(message);

	CollectionJournal collectionJournal = new CollectionJournal();

	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record);
	assertEquals(1, collectionJournal.size());
    }

    @Test
    public void addAll() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	CollectionJournal actualCollectionJournal = new CollectionJournal();
	assertEquals(0, actualCollectionJournal.size());

	actualCollectionJournal.add(collectionJournal);

	assertEquals(2, actualCollectionJournal.size());
	assertEquals(record2, collectionJournal.get(1));
	assertEquals(record1, collectionJournal.get(0));
    }

    @Test
    public void remove() {
	String message = "2014-11-10 20:30:40 ONE источник_сообщение";
	Record record = new Record(message);

	CollectionJournal collectionJournal = new CollectionJournal();

	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record);
	collectionJournal.add(record);
	assertEquals(2, collectionJournal.size());
	collectionJournal.remove(record);
	assertEquals(1, collectionJournal.size());

    }

    @Test
    public void get() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);

	CollectionJournal collectionJournal = new CollectionJournal();

	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record1);
	collectionJournal.add(record2);
	assertEquals(record2, collectionJournal.get(1));
	assertEquals(record1, collectionJournal.get(0));
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void set() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record1);
	collectionJournal.add(record1);
	collectionJournal.add(record1);
	collectionJournal.set(1, record2);
	assertEquals(record2, collectionJournal.get(1));
	assertEquals(record1, collectionJournal.get(0));
	collectionJournal.set(4, record2);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void insert() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record1);
	collectionJournal.add(record1);
	collectionJournal.add(record1);
	collectionJournal.insert(1, record2);
	collectionJournal.insert(0, record2);
	assertEquals(record2, collectionJournal.get(2));
	assertEquals(record2, collectionJournal.get(0));
	collectionJournal.insert(7, record2);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void removeByIndex() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record1);
	collectionJournal.add(record2);
	assertEquals(2, collectionJournal.size());
	collectionJournal.remove(1);
	assertEquals(1, collectionJournal.size());
	collectionJournal.get(1);
    }

    @Test
    public void removeRange() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-18 10:30:40 ONE источник_сообщение";
	String message2 = "2013-11-10 20:30:50 TWO источник_сообщение";
	String message3 = "2015-11-12 23:33:43 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	Record record3 = new Record(message3);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);
	collectionJournal.add(record3);

	collectionJournal.remove(1, 3);
	collectionJournal.add(record1);

	assertEquals(record0, collectionJournal.get(0));
	assertEquals(record3, collectionJournal.get(1));
	assertNotEquals(record1, collectionJournal.get(1));
	assertEquals(2, collectionJournal.size());

    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void removeAll() {
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	assertEquals(0, collectionJournal.size());
	collectionJournal.add(record1);
	collectionJournal.add(record2);
	assertEquals(2, collectionJournal.size());
	collectionJournal.removeAll();
	assertEquals(0, collectionJournal.size());
	collectionJournal.get(1);
    }

    @Test
    public void filterString() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message2 = "2014-11-10 20:30:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	CollectionJournal expectedCollectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);

	assertEquals(expectedCollectionJournal,
		collectionJournal.filter(message0));
    }

    @Test
    public void filterDate() {
	// хз как определять диапазон
	fail("not supported yet");
    }

    @Test
    public void sortByDate() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 ONE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	collectionJournal.sortByDate();
	assertEquals(record1, collectionJournal.get(0));
	assertEquals(record0, collectionJournal.get(1));
	assertEquals(record2, collectionJournal.get(2));
    }

    @Test
    public void sortByImportanceDate() {
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 THREE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	collectionJournal.sortByImportanceDate();
	assertEquals(record0, collectionJournal.get(0));
	assertEquals(record2, collectionJournal.get(1));
	assertEquals(record1, collectionJournal.get(2));
    }

    @Test
    public void sortByImportanceSourceDate() {
	// а каой приоритет у источника? иначе все тоже самое, что и у
	// предыдущенго метода
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 THREE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	collectionJournal.sortByDate();
	assertEquals(record0, collectionJournal.get(0));
	assertEquals(record2, collectionJournal.get(1));
	assertEquals(record1, collectionJournal.get(3));
    }

    @Test
    public void sortBySourceDate() {
	// а каой приоритет у источника? иначе все тоже самое, что и у метода
	// sortByDate
	String message0 = "2014-11-10 10:30:40 ONE источник_сообщение";
	String message1 = "2014-11-06 12:30:40 ONE источник_сообщение";
	String message2 = "2014-11-20 22:35:40 TWO источник_сообщение";
	Record record0 = new Record(message0);
	Record record1 = new Record(message1);
	Record record2 = new Record(message2);
	CollectionJournal collectionJournal = new CollectionJournal();
	collectionJournal.add(record0);
	collectionJournal.add(record1);
	collectionJournal.add(record2);

	collectionJournal.sortByDate();
	assertEquals(record1, collectionJournal.get(0));
	assertEquals(record0, collectionJournal.get(1));
	assertEquals(record2, collectionJournal.get(2));
    }
}
